<?php if(!defined('OCTOMS')){header('HTTP/1.1 403');die('{"error":"forbidden"}');}
/*
 * @package       OctoMS.com
 * @subpackage	  DesignJotter
 * @link          http://DesignJotter.com
 * @copyright     Copyright 2012, Valentino-Jivko Radosavlevici
 * 
 * Redistributions of files must retain the above copyright notice.
 * 
 * @since         OctoMS 0.0.1, WP-OctoMS 2.0.1
 */
	
	/*
	 * Other - Model
	 * 
	 * @package OctoMS
	 * @subpackage WP-OctoMS
	 * @version 0.1
	 * 
	 * @author Valentino-Jivko Radosavlevici
	 */
	class the_framework_m extends octoms
	{
		/**
		 * Constructor
		 * 
		 * @example 
		 * 
		 * @param 
		 * @return 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function __construct()
		{
			// Get the settings library
			$this->_('settings',OMS_L);
			
			// Get the CSV core library
			$this->_('csv',OMS_CL);
			
		}// end function __construct()
		/**
		 * Save the content
		 * 
		 * @example 
		 * 
		 * @param 
		 * @return 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function save_content()
		{
			// Get the data
			if(null !== $data = $this->settings->get())
			{
				// Open the CSV file
				$csv = $this->csv->open(APP_ROOT.DS.'stages'.DS.'install'.DS.'wp_options.csv');
				
				// Clean it
				$csv->csv = array();
				
				// Append the data
				foreach ($data AS $key => $value)
				{
					$csv->csv[] = array(
						0,
						0,
						$this->settings->prefix.$key,
						$value,
						'yes',
						null
					);
				}
				
				// Save the CSV
				$csv->save();
				
				// Send the message
				$this->template->_api('Current theme content saved successfully!');
			}
			else 
			{
				// Send the message
				$this->template->_api('No options to save.','warning');
			}
			
		}// end function save_content()
		
		/**
		 * Advanced settings
		 * 
		 * 
		 * @author Valentino-Jivko Radosavlevici
		 */
		function advanced()
		{
			// Save the "Under the hood"
			if(null != $v = $this->input->post('uth'))
			{
				// Clean the value
				$v = strval($v);
				if (in_array($v, array('0','1')))
				{
					// Change the constant
					if(!$this->_replace_const('debug_wizard',$v))
					{
						$this->template->_api('Could not rewrite the constants file.',false);
						return;
					}
					
					// Activate the hood
					if ($v == '1')
					{
						// Copy the required files
						foreach(array('.htaccess','php.ini','php5.ini') AS $k => $f)
						{
							if (file_exists(STAGES.DS.'install'.DS.$k.'.bkp'))
							{
								if(!copy(STAGES.DS.'install'.DS.$k.'.bkp', ROOT.DS.$f))
								{
									$this->template->_api('Could not move file '.$f.' to the root.',false);
									return;
								}
							}
						}
						
						// All went well
						$this->template->_api('The "Under the Hood" feature was activated successfully!');
					}
					// Deactivate the hood
					else 
					{
						// Remove the redundant files
						foreach(array('php.ini','php5.ini','.htaccess') AS $f)
						{
							if (file_exists(ROOT.DS.$f))unlink(ROOT.DS.$f);
						}
						
						// All went well
						$this->template->_api('The "Under the Hood" feature was deactivated successfully!');
					}
					
					// Refresh the page
					$this->template->_api(array('refresh'));
				}
				else 
				{
					$this->template->_api('Invalid value. Pleaase refresh and try again.',false);
				}
			}
			
			// Save the "Error wizard"
			if(null != $v = $this->input->post('err'))
			{
				// Clean the value
				$v = strval($v);
				if (in_array($v, array('0','1')))
				{
					// Change the constant
					if(!$this->_replace_const('error_wizard',$v))
					{
						$this->template->_api('Could not rewrite the constants file.',false);
						return;
					}
					
					// All went well
					if ($v == '1')
					{
						$this->template->_api('The Error Wizard was activated successfully!');
					}
					else 
					{
						$this->template->_api('The Error Wizard was deactivated successfully!');
					}
				}
				else 
				{
					$this->template->_api('Invalid value. Pleaase refresh and try again.',false);
				}
			}
			
			// Save the "Environment"
			if(null != $v = $this->input->post('env'))
			{
				// Clean the value
				$v = strval($v);
				if (in_array($v, array('0','1')))
				{
					// Change the constant
					if(!$this->_replace_const('environment',$v))
					{
						$this->template->_api('Could not rewrite the constants file.',false);
						return;
					}
					
					// All went well
					if ($v == '1')
					{
						// Deactivate the hood
						foreach(array('php.ini','php5.ini','.htaccess') AS $f)
						{
							if (file_exists(ROOT.DS.$f))unlink(ROOT.DS.$f);
						}
						
						// All done
						$this->template->_api('Environment changed to Production!');
					}
					else 
					{
						// Activate the hood
						if (DEBUG_WIZARD)
						{
							foreach(array('.htaccess','php.ini','php5.ini') AS $k => $f)
							{
								if (file_exists(STAGES.DS.'install'.DS.$k.'.bkp'))
								{
									if(!copy(STAGES.DS.'install'.DS.$k.'.bkp', ROOT.DS.$f))
									{
										$this->template->_api('Could not move file '.$f.' to the root.',false);
										return;
									}
								}
							}
						}
						
						// All done
						$this->template->_api('Environment changed to Development!');
					}
					
					// Refresh the page
					$this->template->_api(array('refresh'));
				}
				else 
				{
					$this->template->_api('Invalid value. Pleaase refresh and try again.',false);
				}
			}
		}// end function advanced()
		
		/**
		 * Replace application constants
		 * This is a dangerous function;
		 * 
		 * @return boolean - True for success, False for failure
		 * @author Valentino-Jivko Radosavlevici
		 */
		function _replace_const($name=null,$value=null)
		{
			// Name and value are mandatory
			if ($value == null || $name == null)
			{
				return false;
			}
			else 
			{
				// Clean the input
				if (in_array($name, array('environment','debug_wizard','error_wizard')))
				{
					// Clean the value
					$value = strval($value);
					
					// Valid value?
					if ($value === '0' || $value === '1')
					{
						// Uppercase
						$name = strtoupper($name);
						
						// Get the file contents
						if(false !== $c = file_get_contents($f = APP_CONFIG.DS.'constants'.EXT))
						{
							// Replace the content
							$c = preg_replace(
								'/(define[\s]?\([\s]?[\'"]{1}'.$name.'[\'"]{1}[\s]?,)[^)]+\)/im',
								'${1}'."\t".$value.')', 
								$c
							);
							
							// Save the change
							if(false !== $fh = fopen($f,'w'))
							{
								// Write the content
								fwrite($fh, $c);
								
								// Close the handler
								fclose($fh);
								
								// All done
								return true;
							}
						}
					}
				}
			}
			
			// Something went wrong
			return false;
			
		}// end function _replace_const()
		
	}// end class other_m
	
	
/* End Of File <other.inc> */